<?php

namespace app\admin\controller;
use app\admin\model\TagModel;
use app\admin\model\UserModel;
use think\Db;

class Tag extends Base
{

    /**
     * [index 留言列表]
     * @author 【朱沪宁】
     */
    public function index(){
        $map = [];
        $rolename=session('rolename');
        $this->assign('showBut',$rolename=='超级管理员'?1:0);
        $key = input('key');
        if($key&&$key!==""){
            $map['name'] = ['like',"%" . $key . "%"];
        }
   
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('tag')
            ->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $tag = new TagModel();
        $lists = $tag->getTagByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        if(input('get.page')){
            return json($lists);
        }
//        print_r($lists);die;
        return $this->fetch();
    }
    /**
     * [add_tag 添加文章]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function add_tag()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $param['uid']=session('uid');
            $tag = new TagModel();
            $flag = $tag->insertTag($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        return $this->fetch();
    }


    /**
     * [edit_tag 编辑文章]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function edit_tag()
    {
        $tag = new TagModel();
        if(request()->isAjax()){

            $param = input('post.');
            // $param["create_time"] = strtotime($param["create_time"]);  
            $flag = $tag->updateTag($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.tag_id');
        $tag = $tag->getOneTag($id);

        $this->assign('tag',$tag);
        return $this->fetch();
    }
    /**
     * [del_tag 删除留言]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function del_tag()
    {
        $id = input('param.id');
        $cate = new TagModel();
        $flag = $cate->delTag($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

}